{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4d3de406",
   "metadata": {},
   "source": [
    "# Set\n",
    "集合用于在单个变量中存储多个项目。\n",
    "\n",
    "Set 是 Python 中用于存储数据集合的 4 种内置数据类型之一，另外 3 种是 List、Tuple 和 Dictionary，它们的性质和用法各不相同。\n",
    "\n",
    "集合是无序和无索引的集合。\n",
    "\n",
    "集合是用大括号写的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d3a1012d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'cherry', 'banana'}\n"
     ]
    }
   ],
   "source": [
    "thisset = {\"apple\", \"banana\", \"cherry\"}\n",
    "print(thisset)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "890cf06b",
   "metadata": {},
   "source": [
    "## 集合项目\n",
    "设置项是无序的、不可更改的，并且不允许重复值。\n",
    "\n",
    "### 无序\n",
    "无序意味着集合中的项目没有定义的顺序。\n",
    "\n",
    "每次使用时，集合项可以以不同的顺序出现，并且不能通过索引或键引用。\n",
    "\n",
    "### 不可改变的\n",
    "集合是不可更改的，这意味着我们无法在创建集合后更改项目。\n",
    "\n",
    "创建集合后，您无法更改其项目，但可以添加新项目。\n",
    "\n",
    "### 不允许重复\n",
    "集合不能有两个具有相同值的项目。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "80259cce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'cherry', 'banana'}\n"
     ]
    }
   ],
   "source": [
    "thisset = {\"apple\", \"banana\", \"cherry\", \"apple\"}\n",
    "\n",
    "print(thisset)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5555ed63",
   "metadata": {},
   "source": [
    "## 获取集合的长度\n",
    "要确定一个集合有多少个项目，请使用 len() 方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3f34c2d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "thisset = {\"apple\", \"banana\", \"cherry\"}\n",
    "\n",
    "print(len(thisset))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9028d450",
   "metadata": {},
   "source": [
    "## 集合项 - 数据类型\n",
    "集合项可以是任何数据类型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "91cd41ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "set1 = {\"apple\", \"banana\", \"cherry\"}\n",
    "set2 = {1, 5, 7, 9, 3}\n",
    "set3 = {True, False, False}\n",
    "\n",
    "set4 = {\"abc\", 34, True, 40, \"male\"}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c29a7312",
   "metadata": {},
   "source": [
    "## set() 构造函数\n",
    "也可以使用 set() 构造函数来创建一个集合。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cd5d05f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'cherry', 'banana'}\n"
     ]
    }
   ],
   "source": [
    "thisset = set((\"apple\", \"banana\", \"cherry\")) # 注意双括号\n",
    "print(thisset)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "028cd431",
   "metadata": {},
   "source": [
    "# 访问项目\n",
    "不能通过引用索引或键来访问集合中的项目。\n",
    "\n",
    "但是您可以使用 for 循环遍历集合项，或者使用 in 关键字询问集合中是否存在指定的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1fa7a277",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "cherry\n",
      "banana\n"
     ]
    }
   ],
   "source": [
    "thisset = {\"apple\", \"banana\", \"cherry\"}\n",
    "\n",
    "for x in thisset:\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "728088c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "thisset = {\"apple\", \"banana\", \"cherry\"}\n",
    "\n",
    "print(\"banana\" in thisset)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0aca46ab",
   "metadata": {},
   "source": [
    "## 添加项目\n",
    "\n",
    "要将一项添加到集合中，请使用 add() 方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e80c2ae4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'cherry', 'banana', 'orange'}\n"
     ]
    }
   ],
   "source": [
    "set1 = {\"apple\", \"banana\", \"cherry\"}\n",
    "\n",
    "set1.add(\"orange\")\n",
    "\n",
    "print(set1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91fc825b",
   "metadata": {},
   "source": [
    "## 添加集合\n",
    "要将另一个集合中的项目添加到当前集合中，请使用 update() 方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "58ea82cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'cherry', 'pineapple', 'mango', 'banana', 'papaya'}\n"
     ]
    }
   ],
   "source": [
    "set1 = {\"apple\", \"banana\", \"cherry\"}\n",
    "set2 = {\"pineapple\", \"mango\", \"papaya\"}\n",
    "\n",
    "set1.update(set2)\n",
    "\n",
    "print(set1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6fbad9c",
   "metadata": {},
   "source": [
    "## 添加任何可迭代对象\n",
    "update() 方法中的对象不必是一个集合，它可以是任何可迭代对象（元组、列表、字典等）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "49a725d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'cherry', 'orange', 'kiwi', 'banana'}\n"
     ]
    }
   ],
   "source": [
    "set1 = {\"apple\", \"banana\", \"cherry\"}\n",
    "set2 = [\"kiwi\", \"orange\"]\n",
    "\n",
    "set1.update(set2)\n",
    "\n",
    "print(set1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "728b122a",
   "metadata": {},
   "source": [
    "## 更改项目\n",
    "创建集合后，无法更改其项目，但可以添加新项目。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "400f6afd",
   "metadata": {},
   "source": [
    "## 除去项目\n",
    "要删除集合中的项目，请使用 remove() 或 discard() 方法。\n",
    "\n",
    "如果要删除的项目不存在，remove() 将引发错误。\n",
    "\n",
    "如果要删除的项目不存在，discard() 将不会引发错误。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a49465cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'cherry'}\n"
     ]
    }
   ],
   "source": [
    "set1 = {\"apple\", \"banana\", \"cherry\"}\n",
    "\n",
    "set1.remove(\"banana\")\n",
    "\n",
    "print(set1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "926c8ab2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'cherry'}\n"
     ]
    }
   ],
   "source": [
    "set1 = {\"apple\", \"banana\", \"cherry\"}\n",
    "\n",
    "set1.discard(\"banana\")\n",
    "\n",
    "print(set1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36b93379",
   "metadata": {},
   "source": [
    "还可以使用 pop() 方法删除一个项目，但此方法将删除最后一个项目。 请记住，集合是无序的，因此您将不知道删除了哪些项目。\n",
    "\n",
    "pop() 方法的返回值是移除的项。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "262f51c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "{'cherry', 'banana'}\n"
     ]
    }
   ],
   "source": [
    "set1 = {\"apple\", \"banana\", \"cherry\"}\n",
    "\n",
    "x = set1.pop()\n",
    "\n",
    "print(x)\n",
    "\n",
    "print(set1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3af6add0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set()\n"
     ]
    }
   ],
   "source": [
    "set1 = {\"apple\", \"banana\", \"cherry\"}\n",
    "\n",
    "set1.clear()\n",
    "\n",
    "print(set1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "889e355e",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'set1' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-22-5d444dd2b796>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mset1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mset1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'set1' is not defined"
     ]
    }
   ],
   "source": [
    "set1 = {\"apple\", \"banana\", \"cherry\"}\n",
    "\n",
    "del set1\n",
    "\n",
    "print(set1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ffe5a78",
   "metadata": {},
   "source": [
    "# 循环项目\n",
    "可以使用 for 循环遍历设置项："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "78c5a857",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "cherry\n",
      "banana\n"
     ]
    }
   ],
   "source": [
    "thisset = {\"apple\", \"banana\", \"cherry\"}\n",
    "\n",
    "for x in thisset:\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef29e09d",
   "metadata": {},
   "source": [
    "# 连接集合\n",
    "## 连接两个集合\n",
    "在 Python 中有几种方法可以连接两个或多个集合。\n",
    "\n",
    "可以使用 union() 方法返回一个包含两个集合中所有项的新集合，或者使用 update() 方法将一个集合中的所有项插入到另一个集合中："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1acc2c1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 'c', 2, 3, 'b', 'a'}\n"
     ]
    }
   ],
   "source": [
    "set1 = {\"a\", \"b\" , \"c\"}\n",
    "set2 = {1, 2, 3}\n",
    "\n",
    "set3 = set1.union(set2)\n",
    "print(set3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e99228cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 'c', 2, 3, 'b', 'a'}\n"
     ]
    }
   ],
   "source": [
    "set1 = {\"a\", \"b\" , \"c\"}\n",
    "set2 = {1, 2, 3}\n",
    "\n",
    "set1.update(set2)\n",
    "print(set1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
